<?php

/**
 * User: day
 * Date: 2018/08/07
 * Time: 23:04
 * Description:
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\EBETAPIService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;


class EBETOrderProcessor extends BaseOrderProcessor {
    protected $signature = self::PREFIX . 'ebet {--type=}';
    protected $description = 'EBET注单拉取';

    public $ebetService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::EBET_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::EBET_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;
    
    const REPORT_TIME_DIFFERENCE = 8 * 60 * 60; //报表使用北京时间时区
    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->ebetService = new EBETAPIService();
        $this->partnerName = "EBET";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_LIVE;
        $this->reportOrderType = self::GAME_TYPE_LIVE;
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval);
//        dd([date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', $endTime)]);
        $data = $this->ebetService->getEBETOrders($startTime, $endTime);
        return $data;
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        $this->originData = json_encode($data);
        $this->orderNumber = $data["betHistoryId"];
        $this->betTime = date('Y-m-d H:i:s', $data['createTime']);
        $this->betMoney = round(amount_format($data['bet'],true), 0);//投注额（下注总额）
        //ebet没有注单状态，这里以结算时间来判断
        if($data['payoutTime']){
            $this->validMoney = round(amount_format($data['validBet'],true), 0);//有效投注
            $this->prizeMoney = round(amount_format($data['payout'],true), 0);//派奖
            $this->winLossMoney = $this->prizeMoney - $this->betMoney;//输赢(派奖减去总投注额)
            $this->settleTime = date('Y-m-d H:i:s', $data['payoutTime']);//结算时间
            $this->commonStatus = self::COMMON_STATUS_SETTLE;//注单状态:2=结算
        }else{
            $this->commonStatus = self::COMMON_STATUS_INVALID;//注单状态:0=等待
        }
        $this->apiUserName = $data['username'];//第三方用户名
        $this->contributeMoney = 0; //贡献  ebet接口没有这个值
        $this->bonusMoney = 0; //奖金，ebet没有这个值
        $this->updateKey = $this->betTime;//updateKey（可以根据这个字段重新抓取数据）
        $this->reportTime = date('Y-m-d H:i:s', $data['createTime'] + self::REPORT_TIME_DIFFERENCE);//第三方计算该注单的报表时间,北京时间
        $this->apiStatus = 1;
        $this->gameCode = 0;//ebet没有提供游戏列表接口
        $this->gameName = $this->ebetService::GAMENAME[$data['gameType']];
        $this->betContent = $this->ebetService::getBetConetnt($data);//下注项
        $this->platform = $this->ebetService::ORIGIN[$data['platform']];//来源
    }

    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey){
        Redis::connection()->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }
}
